﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using WeFrame.Core.Common;
using WeFrame.Core.BLL;
using WeFrame.Core.Model;

namespace WeFrame.Web.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TextBoxUserName.Text = Utils.GetCookie("RememberName");
            }
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            string userName = TextBoxUserName.Text.Trim();
            string userPassword = TextBoxPassword.Text.Trim();

            if (userName.Equals("") || userPassword.Equals(""))
            {
                this.MessageTip.InnerHtml = "请输入用户名或密码";
                return;
            }


            //利用回话记录登陆次数 并判断登录错误次数
            if (Session["AdminLoginCount"] == null)
            {
                Session["AdminLoginCount"] = 1;
            }
            else
            {
                Session["AdminLoginCount"] = Convert.ToInt32(Session["AdminLoginCount"]) + 1;
            }

            if (Session["AdminLoginCount"] != null && Convert.ToInt32(Session["AdminLoginCount"]) > 5)
            {
                this.MessageTip.InnerHtml = "错误超过5次，关闭浏览器重新登录！";
                return;
            }

            SystemAdminBLL bll = new SystemAdminBLL();
            SystemAdminEntity model = bll.GetModel(userName, userPassword, true);
            if (model == null)
            {
                this.MessageTip.InnerHtml = "用户名或密码有误，请重试！";
                return;
            }

            Session[WeFrameKey.SESSION_ADMIN] = model;
            Session.Timeout = 45;

            //写入Cookies
            Utils.WriteCookie("RememberName", model.UserName, 14400);
            Utils.WriteCookie("AdminName",model.UserName);
            Utils.WriteCookie("AdminPassword",model.Password);
            
            Response.Redirect("Index.aspx");
            return;
        }

    }
}